package com.jrx.anytxn.transaction.job.coupon.config;

import com.jrx.anytxn.customer.entity.TlRightsExchangeLog;
import com.jrx.anytxn.param.enums.OrderStateEnum;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.job.coupon.step.CouponOrderStateUpdateProcessor;
import com.jrx.anytxn.transaction.job.coupon.step.CouponOrderStateUpdateWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName CouponOrderStateUpdateConfiguration
 *
 * @author lujianyou
 * @version 1.0
 * @description 权益订单状态更新 批处理
 * @date 2021/11/29 10:33
 **/
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
@EnableBatchProcessing
public class CouponOrderStateUpdateConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(CouponOrderStateUpdateConfiguration.class);

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private CouponOrderStateUpdateProcessor orderStateUpdateProcessor;
    @Autowired
    private CouponOrderStateUpdateWriter orderStateUpdateWriter;

    @Bean
    public Job couponOrderStateUpdateJob(){
        return jobBuilderFactory.get("couponOrderStateUpdateJob")
                .incrementer(new RunIdIncrementer())
                .start(couponOrderStateUpdateStep())
                .build();
    }

    @Bean
    public Step couponOrderStateUpdateStep() {
        return stepBuilderFactory.get("couponOrderStateUpdateStep")
                .<TlRightsExchangeLog, TlRightsExchangeLog>chunk(anytxnBatchProperties.getDefaultConfig().getChunkLimit())
                //表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(couponOrderStateUpdateReader())
                .processor(orderStateUpdateProcessor)
                .writer(orderStateUpdateWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                //多线程
                .throttleLimit(anytxnBatchProperties.getDefaultConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlRightsExchangeLog> couponOrderStateUpdateReader() {
        MyBatisCursorItemReader<TlRightsExchangeLog> itemStreamReader = new MyBatisCursorItemReader<>();
        itemStreamReader.setSaveState(false);
        itemStreamReader.setSqlSessionFactory(sqlSessionFactory);
        //查询方法
        itemStreamReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtTlRightsExchangeLogMapper." +
                "selectByTenantIdAndOrderStateAndChannel");
        Map<String,Object> map = new HashMap<>(4);

        //查询参数设置
        map.put("tenantId","101");
        map.put("orderState", OrderStateEnum.PROCESSING.getCode());
        map.put("channel","channel01");
        itemStreamReader.setParameterValues(map);

        SynchronizedItemStreamReader<TlRightsExchangeLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(itemStreamReader);
        return synchronizedItemStreamReader;
    }
}
